#include "Face.h"

Face::Face(int index, vector<Vertex*> vertices, int sharpness, bool opt) {
    this->index = index;
    this->sharpness = sharpness;
    this->vertices = vertices;
    this->opt = opt;

    ccVertex = 0;
}

Vector3d Face::centroid() const {
    Vector3d result = Vector3d(0, 0, 0);
    for (unsigned int i = 0; i < vertices.size(); i++) {
        result += vertices[i]->pos;
    }
    result /= vertices.size();
    return result;
}

void Face::setCCVertex(Vertex* v) {
    ccVertex = v;
    ccVertex->pos = centroid();
    if (sharpness > 0) {
        ccVertex->sharpness = sharpness - 1;
    } else {
        ccVertex->sharpness = sharpness;
    }
    if (opt) {
        ccVertex->opt = true;
    } else {
        ccVertex->opt = false;
        for (unsigned int i = 0; i < vertices.size(); i++) {
            if (vertices[i]->opt) ccVertex->opt = true;
        }
    }
}

Vertex* Face::getDiagonal(const Vertex* v) const {
    if (vertices.size() != 4) return 0;

    for (unsigned int i = 0; i < vertices.size(); i++) {
        if (vertices[i] == v) {
            return vertices[(i+2) % 4];
        }
    }

    return 0;
}

Vertex* Face::getFromVertex(const Vertex* v, int offset) const {
    for (unsigned int i = 0; i < vertices.size(); i++) {
        if (vertices[i] == v) {
            return vertices[(i+offset) % vertices.size()];
        }
    }

    return 0;
}

void Face::geometry(bool useMeanCurvature) const {
    for (unsigned int i = 0; i < vertices.size(); i++) {
        vertices[i]->vertexGeometry(useMeanCurvature);
    }
}
